package com.zrgk.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zrgk.bean.ConContentBean;
import com.zrgk.service.ConContentService;
import com.zrgk.util.PageUtil;

@Controller
@RequestMapping(value= "/conContent.do")
public class ConContentController {
	@Autowired
	@Qualifier(value = "conContentService")
	private ConContentService conContentService;

	@RequestMapping(params = "method=list")
	public String listConContent(HttpServletRequest request) {
		// 设置分页信息
		//Page<ConContentBean> page = PageHelper.startPage(1, 5);
        String nowPage = request.getParameter("nowPage");
		
		PageUtil pageUtil=new PageUtil();
		if(nowPage!=null&&nowPage!=""){
			pageUtil.setNowPage(Integer.parseInt(nowPage));	
		}
		
		Map<String, String> map=new HashMap<String, String>();
		String contentName = request.getParameter("contentName");
		if(contentName!=null&&contentName!=""){
			map.put("contentName", contentName);
		}
		
		String telephone = request.getParameter("telephone");
		if(telephone!=null&&telephone!=""){
			map.put("telephone", telephone);
		}
        map.put("method", "list");
		pageUtil.setSearchInfo(map);
		
		conContentService.findConContentByPage(pageUtil);
		request.setAttribute("pageUtil", pageUtil);
		return "/html/resource/customer/resource/list.jsp";
	}

	

	@RequestMapping(params = "method=insert")
	public String insertConContent(HttpServletRequest request,ConContentBean cocon) {
		ConContentBean concon = new ConContentBean();
	    concon.setConsumername(concon.getConsumername());
	    concon.setSex(concon.getSex());
	    Date date = new Date();
	    concon.setBirthday(date);
	    concon.setWriteTime(date);
	    concon.setEmail(concon.getEmail());
	    concon.setTelephone(concon.getTelephone());
	    concon.setOfficePhone(concon.getOfficePhone());
	    concon.setHomePhone(concon.getHomePhone());
	    concon.setType(concon.getType());
	    concon.setState(concon.getState());
	    concon.setContaceAddress(concon.getContaceAddress());
	    concon.setRemark(concon.getRemark());
	    concon.setKHstate(concon.getKHstate());
	    conContentService.insertConContent(concon);
	    	    
		return "/html/resource/sys/employee/result.jsp";
	}
	@RequestMapping(params="method=findConContentById")
	public String findConContentById(HttpServletRequest request,int id){
		ConContentBean cocon = conContentService.findConContentById(id);
		request.setAttribute("cocon", cocon);
		return "/html/resource/customer/resource/list.jsp";
	}
	
	@RequestMapping(params="method=updateConContent")
	public String updateConContent(HttpServletRequest request,int id){
		ConContentBean cocon = conContentService.findConContentById(id);
		request.setAttribute("cocon", cocon);
		return "/html/resource/customer/resource/list.jsp";
	}

	@RequestMapping(params="method=updateContentState")
	public void updateContentState(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			String state = request.getParameter("state");
			String id = request.getParameter("id");
			ConContentBean concon=new ConContentBean();
			concon.setContentId(Integer.parseInt(id));
			if(state.equals("0")){
				concon.setState(1);
			}else{
				concon.setState(0);
			}
			conContentService.updateContentState(concon);
			out.print("true");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**客户添加
	 * 
	 * @param request
	 * @param empId
	 * @return
	 */
	@RequestMapping(params="method=updateContentBefore")
	public String updateContentBefore(HttpServletRequest request,String contentId){
		ConContentBean concon = conContentService.findConContentById(Integer.parseInt(contentId));
		request.setAttribute("concon", concon);
		return "/html/resource/customer/resource/add.jsp";
	}

	public ConContentService getConContentService() {
		return conContentService;
	}

	public void setConContentService(ConContentService conContentService) {
		this.conContentService = conContentService;
	}
}